﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Xml.Serialization;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace WIAssistant
{
    public static class QueryResultsHelper
    {
        private static List<WorkItemColumnItem> GetQueryColumns(WorkItemColumnTypes columnFields, DisplayFieldList fields, WorkItem workItem)
        {
            var row = new List<WorkItemColumnItem>();
            foreach (FieldDefinition column in fields)
            {
                // Default to showing the field
                Visibility visibility = new Visibility();
                visibility = Visibility.Visible;

                // See if this field should be shown.
                FieldDefinition definition = column;
                var foundDisplayInfo = columnFields.Find(x => x.Id == definition.Id);

                if (foundDisplayInfo != null)
                    if (!foundDisplayInfo.IsVisible)
                    {
                        // Don't show this one if we have a valid match and it should not be Visible.
                        visibility = Visibility.Collapsed;
                    }

                // See if the column we are looking at exists in the WorkItem we are trying to display
                object valueForWorkItem;
                if (workItem.Fields.Contains(column.Name))
                    valueForWorkItem = workItem[column.Name];
                else
                    valueForWorkItem = null;

                var workItemColumn = new WorkItemColumnItem
                                         {
                                             Name = column.Name,
                                             Value = valueForWorkItem,
                                             FieldID = column.Id,
                                             WorkItemForColumn = workItem, 
                                             ItemVisibility = visibility
                                         };
                row.Add(workItemColumn);
            }

            return row;
        }

        public static List<List<WorkItemColumnItem>> GetDisplayValues(this WorkItemCollection queryResults, WorkItemColumnTypes columnFields)
        {
            var result = new List<List<WorkItemColumnItem>>();
            foreach (WorkItem workItem in queryResults)
            {
                var row = GetQueryColumns(columnFields, queryResults.DisplayFields, workItem);
                result.Add(row);
            }
            return result;
        }

        // Get a dictionary of the columns with the keys being display values.
        public static WorkItemColumnTypes GetColumnTitles(this WorkItemCollection queryResults, string query, string project, string server)
        {
            var result = new WorkItemColumnTypes { TFSServerName = server, TFSProjectName = project, TFSQueryName = query };
            var savedVersion = MiscHelpers.DeserializeObject<WorkItemColumnTypes>(result.GetSerializeFilename());

            foreach (FieldDefinition displayField in queryResults.DisplayFields)
            {
                // Set a default value for the visibility.  If we can get more info from a file we will change it later.
                bool isVisible = true;

                // If the is field has been saved, then use that value for the Is Visible:

                if (savedVersion != null)
                {
                    FieldDefinition closureDisplayField = displayField;
                    WorkItemColumnType savedVersionFound = savedVersion.Find(x => x.Id == closureDisplayField.Id);

                    if (savedVersionFound != null)
                        isVisible = savedVersionFound.IsVisible;
                }

                var workItemColumnType = new WorkItemColumnType
                {
                    Title = displayField.Name,
                    Id = displayField.Id,
                    IsVisible = isVisible
                };

                workItemColumnType.OnVisibleChanged += MainMediator.Instance.WorkItemColumnTypeOnVisibleChanged;

                result.Add(workItemColumnType);


            }
            return result;
        }

    }
}
